import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import VuexPersistence from 'vuex-persist';
const vuexLocal = new VuexPersistence({
	storage: window.sessionStorage
})
import { GetMenus } from '@/api/Rights';

export default new Vuex.Store({
	state: {
		/**
		 * 登录的用户
		 */
		user: {
		},
		/**
		 * 所有的菜单
		 */
		menus: [],
		/**
		 * 我的菜单
		 */
		myMenus: [],
		/**
		 * 产品信息
		 */
		product: {}
	},
	mutations: {
		// 设置用户
		setUser(state, user) {
			state.user = user;
		},
		// 设置token
		setToken(state, token) {
			state.user.Password = token;
		},
		// 设置菜单
		setMenus(state, value) {
			state.menus = value;
		},
		// 设置我的菜单
		setMyMenus(state, value) {
			state.myMenus = value;
		},
		// 设置产品信息
		setProduct(state, value) {
			state.product = value;
		}
	},
	actions: {
		// 获取所有菜单
		async getMenus({ commit }) {
			try {
				const data = await GetMenus();
				const menus = Vue.prototype.$handleMenu(data);
				commit('setMenus', menus);
			} catch (error) {
				console.log(error);
			}
		}
	},
	plugins: [vuexLocal.plugin]
})
